package org.gwu.seuss.client;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.gwu.seuss.client.commandline.ClientValidator;
import org.gwu.seuss.client.commandline.CommandLineArgs;
import org.gwu.seuss.communication.domain.FileTO;
import org.gwu.seuss.communication.domain.UserTO;
import org.gwu.seuss.communication.request.Actions;
import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;
import org.gwu.seuss.communication.util.FileUtils;
import org.gwu.seuss.ui.CommandLineInterface;
import org.gwu.seuss.ui.commandline.BaseCommandLineArgs;
import org.gwu.seuss.ui.connection.SSLConnectionHandler;
import org.gwu.seuss.ui.exception.NoArgumentsProvidedException;
import org.kohsuke.args4j.CmdLineParser;

public class ClientImp extends CommandLineInterface {

	//TODO: should be set in spring 
	private static final String DOWNLOAD_LOCATION = "";

	@Override
	public void init() {
		// TODO: should set up using Spring
		connectionHandler = new SSLConnectionHandler();
		validator = new ClientValidator();
		bcla = new CommandLineArgs();
		parser = new CmdLineParser(bcla);
	}

	@Override
	public String handleServerResponse(ClientRequest cr, ServerResponse sr)
			throws Exception {
		if (cr.getAction().equals(Actions.LIST)) {
			StringBuilder sb = new StringBuilder();
			sb.append("Number of files found: " + sr.getFiles().size() + "\n");
			if (sr.getFiles().size() > 0) {
				sb.append("FileId | Filename" + "\n");
				for (FileTO file : sr.getFiles()) {
					sb.append(file.getFileId() + " | " + file.getFilename()
							+ "\n");
				}
			}
			return sb.toString();
		}
		if (cr.getAction().equals(Actions.DOWNLOAD)) {
			FileUtils.writeFile(sr.getFilename(), DOWNLOAD_LOCATION,
					sr.getFileBytes());
		}
		return sr.getMessage();
	}

	@Override
	public <T extends BaseCommandLineArgs> ClientRequest getClientRequest(T cli)
			throws IOException, NoArgumentsProvidedException {
		ClientRequest cr = setUpClientRequest(cli);
		CommandLineArgs cla = (CommandLineArgs) cli;

		if (cla.getDownload() != null) {
			cr.setAction(Actions.DOWNLOAD);
			cr.setFileName(cla.getDownload());
		} else if (cla.getUpload() != null) {
			cr.setAction(Actions.UPLOAD);
			File f = new File(cla.getUpload());
			cr.setFileName(f.getName());
			cr.setFileBytes(Files.readAllBytes(Paths.get(cla.getUpload())));
		} else if (cla.isFileList()) {
			cr.setAction(Actions.LIST);
		} else if (cla.getNewPassword() != null) {
			cr.setAction(Actions.UPDATE_PASSWORD);
			cr.setUser(new UserTO(cla.getLogin(), cla.getNewPassword()));
		} else if (cla.getDeleteFile() != null) {
			cr.setAction(Actions.DELETE_FILE);
			cr.setFileName(cla.getDeleteFile());
		} else if (cla.getNewEmail() != null) {
			cr.setAction(Actions.UPDATE_EMAIL);
			cr.setUser(new UserTO(cla.getNewEmail()));
		}else {
			throw new NoArgumentsProvidedException();
		}
		return cr;
	}

	@Override
	public String getPropertiesFile() {
		return "client.properties";
	}

}
